package com.one.consumer.user.presenter;

import android.content.Context;

import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.presenter.BaseApiPresenter;
import com.one.common.utils.MyToast;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.base.IView;
import com.one.consumer.order.model.OrderModel;
import com.one.consumer.user.model.UserModel;
import com.one.consumer.user.model.extra.InvoiceExtra;

import java.util.HashMap;
import java.util.Map;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/11/4 2:43 PM
 * description ：索取发票，添加抬头，修改抬头，默认抬头
 **/
public class InvoicePresenter extends BaseApiPresenter<IView, UserModel> {
    private InvoiceExtra extra;
    private Map<String, String> param = new HashMap<>();

    public InvoicePresenter(IView mView, Context mContext) {
        super(mView, mContext, new UserModel((BaseActivity) mContext));
    }

    @Override
    public void onCreate() {
        super.onCreate();
        extra = (InvoiceExtra) mActivity.getIntent().getSerializableExtra(InvoiceExtra.getExtraName());
        if (extra != null && extra.getLookUpItem() != null) {
        }
    }

    /**
     * 提交
     */
    public void submit(String invoice_type, String invoice_name, String invoice_no,
                       String invoice_company_name, String invoice_company_address,
                       String invoice_company_phone, String invoice_bank_name,
                       String invoice_bank_no) {
        if (extra != null && extra.getLookUpItem() != null) {
            // 修改抬头
            param.put("invoice_id", extra.getLookUpItem().getInvoice_id() + "");
        }
        addLookUp(invoice_type, invoice_name, invoice_no,
                invoice_company_name, invoice_company_address, invoice_company_phone,
                invoice_bank_name, invoice_bank_no);
    }

    /**
     * 添加发票抬头
     */
    private void addLookUp(String invoice_type, String invoice_name, String invoice_no,
                           String invoice_company_name, String invoice_company_address,
                           String invoice_company_phone, String invoice_bank_name,
                           String invoice_bank_no) {
        param.putAll(getParams(invoice_type, invoice_name, invoice_no,
                invoice_company_name, invoice_company_address, invoice_company_phone,
                invoice_bank_name, invoice_bank_no));
        if (param.size() > 1) {
            mModel.addInvoiceLookUp(param, new ObserverOnResultListener<Object>() {
                @Override
                public void onResult(Object o) {
                    Toaster.showLongToast("添加成功", MyToast.SUCCESS);
                    if (mView != null) {
                        mView.finishPage();
                    }
                }
            });
        }

    }

    /**
     * 检查抬头参数是否合法
     */
    private Map<String, String> getParams(String invoice_type, String invoice_name, String invoice_no,
                                          String invoice_company_name, String invoice_company_address,
                                          String invoice_company_phone, String invoice_bank_name,
                                          String invoice_bank_no) {
        Map<String, String> params = new HashMap<>();

        if (invoice_type.equals("2")) { // 公司
            if (StringUtils.isBlank(invoice_company_name)) {
                Toaster.showLongToast("请输入企业名称");
                return params;
            } else if (StringUtils.isBlank(invoice_no)) {
                Toaster.showLongToast("请输入企业税号");
                return params;
            } else if (StringUtils.isBlank(invoice_company_address)) {
                Toaster.showLongToast("请输入企业注册地址");
                return params;
            } else if (StringUtils.isBlank(invoice_company_phone)) {
                Toaster.showLongToast("请输入企业电话号码");
                return params;
            } else if (StringUtils.isBlank(invoice_bank_name)) {
                Toaster.showLongToast("请输入企业开户银行");
                return params;
            } else if (StringUtils.isBlank(invoice_bank_no)) {
                Toaster.showLongToast("请输入企业开户银行账号");
                return params;
            }
            params.put("invoice_type", invoice_type);
            params.put("invoice_no", invoice_no);
            params.put("invoice_company_name", invoice_company_name);
            params.put("invoice_company_address", invoice_company_address);
            params.put("invoice_company_phone", invoice_company_phone);
            params.put("invoice_bank_name", invoice_bank_name);
            params.put("invoice_bank_no", invoice_bank_no);
        } else { // 个人
            if (StringUtils.isBlank(invoice_name)) {
                Toaster.showLongToast("请输入发票抬头");
                return params;
            }
            params.put("invoice_type", invoice_type);
            params.put("invoice_name", invoice_name);
        }

        return params;
    }

    /**
     * 检查发票参数是否合法
     */
    private Map<String, String> getInvoiceParam(String contactName, String contactPhone, String contactAddress) {
        Map<String, String> params = new HashMap<>();

        if (StringUtils.isBlank(contactName)) {
            Toaster.showLongToast("请输入收件人姓名");
            return params;
        } else if (StringUtils.isBlank(contactPhone)) {
            Toaster.showLongToast("请输入收件人电话");
            return params;
        } else if (StringUtils.isBlank(contactAddress)) {
            Toaster.showLongToast("请输入收件人地址");
            return params;
        }

        // 设置收件人信息
        params.put("invoice_member", contactName);
        params.put("invoice_member_phone", contactPhone);
        params.put("invoice_member_address", contactAddress);
        return params;
    }

//    /**
//     * 获取默认抬头
//     */
//    public void getDefaultLookUp(){
//        mModel.getLookUpDefault(new ObserverOnResultListener<LookUpItem>() {
//            @Override
//            public void onResult(LookUpItem lookUpItem) {
//                if(mView!=null){
//                    mView.setDefaultLookUp(lookUpItem);
//                }
//            }
//        });
//    }

    /**
     * 提交发票参数
     */
    public void submitInvoice(String contactName, String contactPhone,
                              String contactAddress) {

        Map<String, String> map2 = getInvoiceParam(contactName, contactPhone, contactAddress);
        if (map2.size() > 0) {
            if (extra != null && extra.getLookUpItem() != null) {
                param.put("order_id", extra.getOrderId());
                param.put("invoice_id", extra.getLookUpItem().getInvoice_id() + "");
            } else {
                return;
            }
            param.putAll(map2);
            new OrderModel(mActivity).applyInvoice(param, new ObserverOnResultListener<Object>() {
                @Override
                public void onResult(Object o) {
                    Toaster.showLongToast("操作成功！");
                    if(mView!=null){
                        mView.finishPage();
                    }
                }
            });
        }

    }
}
